import { Component } from '@angular/core';
import { IonicPage,ViewController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../../providers/http/http';
import { StorageProvider } from '../../../providers/storage/storage';
import { DialogProvider } from '../../../providers/dialog/dialog';
/**
 * Generated class for the Modalsearch page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
	selector: 'page-modalsearch',
	templateUrl: 'modalsearch.html',
})
export class ModalsearchPage {

	private type: any; //检索类型

	/**
	 * 输入界面控制
	 */
	inputSelectView = {
		applyDefItems: new Array(), //数组
		applyoaminDate: '',
		applyoamaxDate: '',
	}

	/**
	 * 输入界面控制
	 */
	inputView = {
		applyapplycode: false, //流程类型
		applyapplytruename: false, //姓名
		applyapplymember: false, //申请人
		applyapplytitle: false, //文件标题
		applyapplyno: false, //收发文号
		applyapplyurgent: false, //紧急程度
		applyapplycompany: false, //归属公司
		applyapplydepart: false, //归属部门
		applystarttime: false, //开始时间
		applyendtime: false, //截止时间
	}

	/**
	 * 输入界面控制
	 */
	inputViewName = {
		applyapplycode: '流程类型', //流程类流程类型型
		applyapplytruename: '姓名', //姓名
		applyapplymember: '发起人', //申请人
		applyapplytitle: '文件标题', //文件标题
		applyapplyno: '收发文号', //收发文号
		applyapplyurgent: '流程状态', //流程状态
		applyapplycompany: '归属公司', //归属公司
		applyapplydepart: '归属部门', //归属部门
		applystarttime: '开始时间', //开始时间
		applyendtime: '截止时间', //截止时间
	}

	/**
	 * 输入界面控制
	 */
	inputViewModel = {
		applyapplycode: {
			id: '',
			name: '全部流程'
		}, //流程
		applyapplytruename: '', //姓名
		applyapplymember: new Object(), //申请人
		applyapplytitle: '', //文件标题
		applyapplyno: '', //收发文号
		applyapplyurgent: new Object(), //紧急程度
		applyapplycompany: new Object(), //归属公司
		applyapplydepart: new Object(), //归属部门
		applystarttime: '', //开始时间
		applyendtime: '', //截止时间
	}

		constructor(
		public viewCtrl: ViewController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider) {
			this.type = this.navParams.get("type"); //检索类型
		this.ionViewDidParams(this.navParams.get("params"));
		this.inputSelectView.applyoaminDate = '2000-01-01';
		this.inputSelectView.applyoamaxDate = this.getinitDate();
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad Modalsearch');
		//控制显示
		this.ionViewDidShow();
	}

	ionViewDidParams(params) {
		//1.待办事项-行政办公
		if(this.type == 1) {
			this.inputViewModel.applyapplycode = params['applyapplycode'];
			this.inputViewModel.applyapplymember = params['applyapplymember'];
			this.inputViewModel.applystarttime = params['applystarttime'];
			this.inputViewModel.applyendtime = params['applyendtime'];
		}
		//2.收阅传文-行政办公
		if(this.type == 2) {
			this.inputViewModel.applyapplytitle = params['applyapplytitle'];
			this.inputViewModel.applyapplyno = params['applyapplyno'];
			this.inputViewModel.applystarttime = params['applystarttime'];
			this.inputViewModel.applyendtime = params['applyendtime'];
		}
		//3.内部签报-行政办公
		if(this.type == 3) {
			this.inputViewModel.applyapplytitle = params['applyapplytitle'];
			this.inputViewModel.applyapplyno = params['applyapplyno'];
			this.inputViewModel.applyapplyurgent = params['applyapplyurgent'];
			this.inputViewModel.applystarttime = params['applystarttime'];
			this.inputViewModel.applyendtime = params['applyendtime'];
		}
		//4.通知公告-行政办公
		if(this.type == 4) {
			this.inputViewModel.applyapplyurgent = params['applyapplyurgent'];
			this.inputViewModel.applystarttime = params['applystarttime'];
			this.inputViewModel.applyendtime = params['applyendtime'];
		}
		//5.资产管理-行政办公
		if(this.type == 5) {
			this.inputViewModel.applyapplytitle = params['applyapplytitle'];
			this.inputViewModel.applyapplyno = params['applyapplyno'];
		}
		//6.办公用品-行政办公
		if(this.type == 6) {
			this.inputViewModel.applyapplytitle = params['applyapplytitle'];
			this.inputViewModel.applyapplyno = params['applyapplyno'];
		}
		//7.任务查询
		if(this.type == 7) {
			this.inputViewModel.applyapplytitle = params['applyapplytitle'];
			this.inputViewModel.applyapplymember = params['applyapplymember'];
			this.inputViewModel.applyapplycompany = params['applyapplycompany'];
			this.inputViewModel.applyapplydepart = params['applyapplydepart'];
			this.inputViewModel.applystarttime = params['applystarttime'];
			this.inputViewModel.applyendtime = params['applyendtime'];
		}
		//8.在职人员
		if(this.type == 8) {
			this.inputViewModel.applyapplytruename = params['applyapplytruename'];
		}
		//9.休假统计列表
		if(this.type == 9) {
			this.inputViewModel.applyapplytruename = params['applyapplytruename'];
			this.inputViewModel.applyapplycompany = params['applyapplycompany'];
			this.inputViewModel.applyapplydepart = params['applyapplydepart'];
			this.inputViewModel.applystarttime = params['applystarttime'];
			this.inputViewModel.applyendtime = params['applyendtime'];
		}
		//10.待办共通
		if(this.type == 10) {
			this.inputViewModel.applystarttime = params['applystarttime'];
			this.inputViewModel.applyendtime = params['applyendtime'];
		}
	}

	ionViewDidShow() {
		//1.待办事项-行政办公
		if(this.type == 1) {
			this.inputView.applyapplycode = true;
			this.inputView.applyapplymember = true;
			this.inputView.applystarttime = true;
			this.inputView.applyendtime = true;
			this.loaddefData(this.type);
		}
		//2.收阅传文-行政办公
		if(this.type == 2) {
			this.inputView.applyapplytitle = true;
			this.inputView.applyapplyno = true;
			this.inputView.applystarttime = true;
			this.inputView.applyendtime = true;
			this.loaddefData(this.type);
		}
		//3.内部签报-行政办公
		if(this.type == 3) {
			this.inputViewName.applyapplytitle = '标题';
			this.inputViewName.applyapplyno = '编号';
			this.inputView.applyapplytitle = true;
			this.inputView.applyapplyno = true;
			this.inputView.applyapplyurgent = true;
			this.inputView.applystarttime = true;
			this.inputView.applyendtime = true;
			this.loaddefData(this.type);
		}
		//4.通知公告-行政办公
		if(this.type == 4) {
			this.inputViewName.applyapplyurgent = '公告类型';
			this.inputView.applyapplyurgent = true;
			this.inputView.applystarttime = true;
			this.inputView.applyendtime = true;
			this.loaddefData(this.type);
		}
		//5.资产管理-行政办公
		if(this.type == 5) {
			this.inputViewName.applyapplytitle = '资产名称';
			this.inputViewName.applyapplyno = '资产编号';
			this.inputView.applyapplytitle = true;
			this.inputView.applyapplyno = true;
		}
		//6.办公用品-行政办公
		if(this.type == 6) {
			this.inputViewName.applyapplytitle = '办公用品名称';
			this.inputViewName.applyapplyno = '办公用品编号';
			this.inputView.applyapplytitle = true;
			this.inputView.applyapplyno = true;
		}
		//7.任务查询
		if(this.type == 7) {
			this.inputViewName.applyapplytitle = '任务标题';
			this.inputViewName.applyapplymember = '发布人';
			this.inputView.applyapplytitle = true;
			this.inputView.applyapplymember = true;
			this.inputView.applyapplycompany = true;
			this.inputView.applyapplydepart = true;
			this.inputView.applystarttime = true;
			this.inputView.applyendtime = true;
		}
		//8.在职人员
		if(this.type == 8) {
			this.inputView.applyapplytruename = true;
		}
		//9.休假统计列表
		if(this.type == 9) {
			this.inputView.applyapplytruename = true;
			this.inputView.applyapplycompany = true;
			this.inputView.applyapplydepart = true;
			this.inputView.applystarttime = true;
			this.inputView.applyendtime = true;
		}
		//10.待办共通
		if(this.type == 10) {
			this.inputView.applystarttime = true;
			this.inputView.applyendtime = true;
		}
	}

	loaddefData(type: any) {
		//初始化
		let params = new Object();
		if(type == 1)
			params['type'] = 'act_category';
		if(type == 3)
			params['type'] = 'procedure_status';
		this.http.postJson('dict', 'getDictByType.do', params).then(res => {
			let datas = res['data']['dictList'];
			this.inputSelectView.applyDefItems = new Array();
			if(type == 1) {
				this.inputSelectView.applyDefItems.push({
					id: '',
					name: '全部流程'
				});
			} else if(type == 3) {
				this.inputSelectView.applyDefItems.push({
					id: '',
					name: '全部状态'
				});
				this.inputSelectView.applyDefItems.push({
					id: '0',
					name: '审核中'
				});
				this.inputSelectView.applyDefItems.push({
					id: '1',
					name: '完成'
				});
				return;
			} else if(type == 4) {
				this.inputSelectView.applyDefItems.push({
					id: '',
					name: '全部类型'
				});
				this.inputSelectView.applyDefItems.push({
					id: '1',
					name: '部门公告'
				});
				this.inputSelectView.applyDefItems.push({
					id: '2',
					name: '公司公告'
				});
				return;
			}
			for(let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.inputSelectView.applyDefItems.push({
					id: item['id'],
					name: item['value']
				});
			}
		});
	}
	//对比选中
	comparecode(e1: Object, e2: Object) {
		return e1 && e2 ? e1['id'] === e2['id'] : e1 === e2;
	}

	//初始化申请人
	applyapplymemberselect() {
		//共通弹出
		let data = new Object();
		data['selectitem'] = this.inputViewModel.applyapplymember;
		data['multiselect'] = false; //false 单选 true 多选
		let opts = new Object();
		this.dialog.showModal("ModaluserselectPage", data, opts).then(success => {
			//界面关闭的时候
			if(success) {
				let result = success['result'];
				this.inputViewModel.applyapplymember = result[0];
			}
		});
	}

	//初始化申请公司
	applyapplycompanyselect() {
		//共通弹出
		let data = new Object();
		data['selectitem'] = this.inputViewModel.applyapplycompany;
		data['multiselect'] = false; //false 单选 true 多选
		let opts = new Object();
		this.dialog.showModal("ModalcompanyselectPage", data, opts).then(success => {
			//界面关闭的时候
			if(success) {
				let result = success['result'];
				this.inputViewModel.applyapplycompany = result[0];
			}
		});
	}

	//初始化申请部门
	applyapplydepartselect() {
		//共通弹出
		let data = new Object();
		data['selectitem'] = this.inputViewModel.applyapplydepart;
		data['multiselect'] = false; //false 单选 true 多选
		let opts = new Object();
		this.dialog.showModal("ModaldepartselectPage", data, opts).then(success => {
			//界面关闭的时候
			if(success) {
				let result = success['result'];
				this.inputViewModel.applyapplydepart = result[0];
			}
		});
	}

	doback() {
		//关闭modal
		this.viewCtrl.dismiss();
	}

	doclear() {
		//1.待办事项-行政办公
		if(this.type == 1) {
			this.inputViewModel.applyapplycode = {
				id: '',
				name: '全部流程'
			};
			this.inputViewModel.applyapplymember = new Object();
			this.inputViewModel.applystarttime = '';
			this.inputViewModel.applyendtime = '';
		}
		//2.收阅传文-行政办公
		if(this.type == 2) {
			this.inputViewModel.applyapplytitle = '';
			this.inputViewModel.applyapplyno = '';
			this.inputViewModel.applystarttime = '';
			this.inputViewModel.applyendtime = '';
		}
		//3.内部签报-行政办公
		if(this.type == 3) {
			this.inputViewModel.applyapplytitle = '';
			this.inputViewModel.applyapplyno = '';
			this.inputViewModel.applyapplyurgent = new Object();
			this.inputViewModel.applystarttime = '';
			this.inputViewModel.applyendtime = '';
		}
		//4.通知公告-行政办公
		if(this.type == 4) {
			this.inputViewModel.applyapplyurgent = new Object();
			this.inputViewModel.applystarttime = '';
			this.inputViewModel.applyendtime = '';
		}
		//5.资产管理-行政办公
		if(this.type == 5) {
			this.inputViewModel.applyapplytitle = '';
			this.inputViewModel.applyapplyno = '';
		}
		//6.办公用品-行政办公
		if(this.type == 6) {
			this.inputViewModel.applyapplytitle = '';
			this.inputViewModel.applyapplyno = '';
		}
		//7.任务查询
		if(this.type == 7) {
			this.inputViewModel.applyapplytitle = '';
			this.inputViewModel.applyapplymember = new Object();
			this.inputViewModel.applyapplycompany = new Object();
			this.inputViewModel.applyapplydepart = new Object();
			this.inputViewModel.applystarttime = '';
			this.inputViewModel.applyendtime = '';
		}
		//8.在职人员
		if(this.type == 8) {
			this.inputViewModel.applyapplytruename = '';
		}
		//9.休假统计列表
		if(this.type == 9) {
			this.inputViewModel.applyapplytruename = '';
			this.inputViewModel.applyapplycompany = new Object();
			this.inputViewModel.applyapplydepart = new Object();
			this.inputViewModel.applystarttime = '';
			this.inputViewModel.applyendtime = '';
		}
		//10.待办共通
		if(this.type == 10) {
			this.inputViewModel.applystarttime = '';
			this.inputViewModel.applyendtime = '';
		}
	}

	dosearch() {
		let data = new Object();
		//1.待办事项-行政办公
		if(this.type == 1) {
			data['applyapplycode'] = this.inputViewModel.applyapplycode;
			data['applyapplymember'] = this.inputViewModel.applyapplymember;
			data['applystarttime'] = this.inputViewModel.applystarttime;
			data['applyendtime'] = this.inputViewModel.applyendtime;
		}
		//2.收阅传文-行政办公
		if(this.type == 2) {
			data['applyapplytitle'] = this.inputViewModel.applyapplytitle;
			data['applyapplyno'] = this.inputViewModel.applyapplyno;
			data['applystarttime'] = this.inputViewModel.applystarttime;
			data['applyendtime'] = this.inputViewModel.applyendtime;
		}
		//3.内部签报-行政办公
		if(this.type == 3) {
			data['applyapplytitle'] = this.inputViewModel.applyapplytitle;
			data['applyapplyno'] = this.inputViewModel.applyapplyno;
			data['applyapplyurgent'] = this.inputViewModel.applyapplyurgent;
			data['applystarttime'] = this.inputViewModel.applystarttime;
			data['applyendtime'] = this.inputViewModel.applyendtime;
		}
		//4.通知公告-行政办公
		if(this.type == 4) {
			data['applyapplyurgent'] = this.inputViewModel.applyapplyurgent;
			data['applystarttime'] = this.inputViewModel.applystarttime;
			data['applyendtime'] = this.inputViewModel.applyendtime;
		}
		//5.资产管理-行政办公
		if(this.type == 5) {
			data['applyapplytitle'] = this.inputViewModel.applyapplytitle;
			data['applyapplyno'] = this.inputViewModel.applyapplyno;
		}
		//6.办公用品-行政办公
		if(this.type == 6) {
			data['applyapplytitle'] = this.inputViewModel.applyapplytitle;
			data['applyapplyno'] = this.inputViewModel.applyapplyno;
		}
		//7.任务查询
		if(this.type == 7) {
			data['applyapplytitle'] = this.inputViewModel.applyapplytitle;
			data['applyapplymember'] = this.inputViewModel.applyapplymember;
			data['applyapplycompany'] = this.inputViewModel.applyapplycompany;
			data['applyapplydepart'] = this.inputViewModel.applyapplydepart;
			data['applystarttime'] = this.inputViewModel.applystarttime;
			data['applyendtime'] = this.inputViewModel.applyendtime;
		}
		//8.在职人员
		if(this.type == 8) {
			data['applyapplytruename'] = this.inputViewModel.applyapplytruename;
		}
		//9.休假统计列表
		if(this.type == 9) {
			data['applyapplytruename'] = this.inputViewModel.applyapplytruename;
			data['applyapplycompany'] = this.inputViewModel.applyapplycompany;
			data['applyapplydepart'] = this.inputViewModel.applyapplydepart;
			data['applystarttime'] = this.inputViewModel.applystarttime;
			data['applyendtime'] = this.inputViewModel.applyendtime;
		}
		//10.待办共通
		if(this.type == 10) {
			data['applystarttime'] = this.inputViewModel.applystarttime;
			data['applyendtime'] = this.inputViewModel.applyendtime;
		}
		//关闭modal
		this.viewCtrl.dismiss(data);
	}

	//格式化时间
	getinitDate() {
		// 格式化日期，获取今天的日期
		let Dates = new Date(new Date().getTime());
		let year = Dates.getFullYear() + 1;
		let month = (Dates.getMonth() + 1) < 10 ? '0' + (Dates.getMonth() + 1) : (Dates.getMonth() + 1);
		let day = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();

		return year + '-' + month + '-' + day;
	};
}